//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

namespace Microsoft.Research.ScientificWorkflow.UIDesigner.Monitor
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Text;
    using Microsoft.Practices.Unity;
    using Microsoft.Research.eResearch.Blackboard.Common;
    using Microsoft.Research.eResearch.Subscriber.Interfaces;
    using System.IO;

    public class MonitorLoggingService : IDisposable
    {
        private CustomEnterpriseLogger logger;

        private MonitorSettingService monitorSettingService;

        public MonitorLoggingService(IUnityContainer container)
        {
            this.monitorSettingService = container.Resolve<MonitorSettingService>();
            this.monitorSettingService.SettingsChanged += this.MonitorSettingsChanged;
            container.Resolve<IMonitorService>().BlackboardMessageReceived += this.LoggingServiceBlackboardMessageReceived;
        }

        private void MonitorSettingsChanged(object sender, EventArgs e)
        {
            if (this.logger != null)
            {
                this.logger.Close();
            }

            if (this.monitorSettingService.Settings.LogFilePath != null && this.monitorSettingService.Settings.LogFilePath.Length != 0)
            {
                this.logger = new CustomEnterpriseLogger(this.monitorSettingService.Settings.LogFilePath, SourceLevels.All);
            }
        }

        private void LoggingServiceBlackboardMessageReceived(object sender, BlackboardMessageReceivedEventArgs e)
        {
            if (this.logger != null)
            {
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.AppendLine("BlackboardMessage received:");

                foreach (var item in e.IncomingMessage)
                {
                    stringBuilder.Append(item.Key);
                    stringBuilder.Append(',');
                    stringBuilder.AppendLine(item.Value);
                }
                using (Stream stream = new FileStream(this.monitorSettingService.Settings.LogFilePath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite))
                {
                    using (StreamWriter logWriter = new StreamWriter(stream))
                    {
                        logWriter.Write(stringBuilder);
                        logWriter.Flush();
                        logWriter.Close();
                    }
                    stream.Close();
                }
            }
        }

        #region IDisposable Members

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        // The bulk of the clean-up code is implemented in Dispose(bool)
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (this.logger != null)
                {
                    this.logger.Dispose();
                }
            }
        }

        #endregion
    }
}
